package com.ruoyi.web.xxj.contoller;

import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.xxj.domain.Product;
import com.ruoyi.web.xxj.service.IProductService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;

/**
 * 文创产品Controller
 *
 * @author ruoyi
 * @date 2024-02-23
 */
@Api(tags = "文创产品管理")
@RestController
@RequestMapping("/xxj/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    /**
     * 查询文创产品列表
     */

    @ApiOperation("查询文创产品列表")
    @GetMapping("/list")
    public TableDataInfo<Product> list(Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出文创产品列表
     */
    @ApiOperation("导出文创产品列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product) {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "文创产品数据");
    }

    /**
     * 获取文创产品详细信息
     */
    @ApiOperation("获取文创产品详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productService.selectProductById(id));
    }

    /**
     * 新增文创产品
     */
    @ApiOperation("新增文创产品")
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改文创产品
     */
    @ApiOperation("修改文创产品")
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除文创产品
     */
    @ApiOperation("删除文创产品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }

    /******************************************* 以下是小程序接口 ******************************************************/
    /**
     * 查询课程信息列表
     */
    @ApiOperation("[小程序]查询文创信息列表")
    @GetMapping("/miniList")
    public TableDataInfo<Product> miniList() {
        startPage();
        List<Product> list = productService.selectProductList(new Product(String.valueOf(0)));
        return getDataTable(list);
    }
    
    @SuppressWarnings("rawtypes")
    @ApiOperation("[小程序]增加文创查看次数")
    @GetMapping("/miniAddViewCout")
    public AjaxResult addViewCount(@RequestParam(required = true, name = "文创主键ID") Long productId) {
        Product product = productService.selectProductById(productId);
        if (Objects.isNull(product)) {
            throw new ServiceException("无效文创主键ID！");
        }
        product.setViewCount(product.getViewCount() + 1);
        return toAjax(productService.updateProduct(product));
    }
}
